#!/bin/bash -e

function info() {
echo Usage: `basename $0` '[-p path -l id_labdel -t target] cmd'
exit 2
}

args=$*
while getopts  ":p:f:l:t:" opt; do
	case  $opt  in
		p) path=$OPTARG;;
		f) suffix=$OPTARG;;
        l) label=$OPTARG;;
        t) target=$OPTARG;;
        i) ide=$OPTARG;;
        *) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi

if test -z "$ide"; then ide="ID"; fi
if test -z "$path"; then path=`pwd`; fi
if test -z "$label"; then label='running'; fi
if test -z "$target"; then target='/mnt/ilustre/app/medical/.pub/.mnt'; fi 

locat_f=4
pid=.pid.log
done_label=done
delimiter=.
dat='date +%H%M%m%d'
interv=600 # 5 mins
# access, visit time
# modify, content of file is changed. this will update access, modify, change time
# change, file status changed. like chmod, chown can change this time, alse effect modify time

# ctime=change time
# atime=access time
# mtime=modifiy time

# echo "<html><body>"


while true; do

    for i in 30 40 50 60 70 80 90 100 110 120; do
        echo $($dat) ${i} mins not updated.
        for f in $(find $target -type f -mmin +$i -exec basename {} \;); do
            echo "<br/><a href=\"$f\">$f</a>"
        done
        echo "<br/><br/>"
    done > $target/no_update.html
    
    
    for i in 30 20 10; do
        echo $($dat) within ${i} mins updated.
        for f in $(find $target -type f -mmin -$i -exec basename {} \;); do
            echo "<br/><a href=\"$f\">$f</a>"
        done
        echo "<br/><br/>"
    done > $target/update.html
    
    for i in 5; do
        echo $($dat) ${i} mins not updated.
        for f in $(find $target -type f -mmin +$i); do
            f_base=`basename $f`
            locat=`tail -1 $f |cut -f$locat_f`
            if test -s "$locat/$pid"; then
                if
                cat $locat/$pid |grep `echo $f_base|cut -f1 -d$delimiter`|grep $done_label > /dev/null
                then
                echo;echo done $f `tail -1 $f`
                else
                echo;echo undone $f `tail -1 $f`
                fi
            fi
            
        done
        echo;echo
    done > $target/done
    
    
    for i in 5; do
        for f in $(find $target -regex '.*[0-9]+\.[0-9]+\.[0-9].*' -type f -mmin +$i -exec basename {} \;); do
            locat=`tail -1 $f |cut -f$locat_f`
            if test -s "$locat/$pid"; then
                if
                cat $locat/$pid |grep `echo $f|cut -f1 -d$delimiter`|grep $done_label > /dev/null
                then
                echo done `tail -1 $f|cut -f1`
                else
                echo undone `tail -1 $f|cut -f1`
                fi
            fi   
        done
    done |sort -k1,1 -k3,3|sed s/$/\\n/ > $target/stopped
    
    
    for i in 5; do
        for f in $(find $target -regex '.*[0-9]+\.[0-9]+\.[0-9].*' -type f -mmin -$i -exec basename {} \;); do
            echo;echo running `tail -1 $f|cut -f1`
        done
    done > $target/running
    
    qstat|grep -P '(fei\.yang|xuefeng\.zhan)'|awk -F' ' '{print $(NF-1) " "$2}'>/tmp/qs
    cat /tmp/qs >>$target/running

sleep $interv

done

# echo "</body></html>"